define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'csmitsm_xcore', 'csmitsm_xcflow'],
    function ($, undefined, Backend, Table, Form, xcore, xcflow) {

        var Controller = {
            _queryString: '',
            index: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'csmitsm/xcflowmdmdata/index?xcform_id=' + Config.xcform_id + '&' + Controller._queryString,
                        add_url: 'csmitsm/xcflowmdmdata/createOrUpdateXcformPage' + Controller._queryString,
                        edit_url: 'csmitsm/xcflowmdmdata/showXcformPage?xcform_id=' + Config.xcform_id,
                        del_url: 'csmitsm/xcflowmdmdata/del?xcform_id=' + Config.xcform_id,
                        table: Config.noprefixtablename,
                    }
                });
                var table = $("#table");


                var columns = [{
                    checkbox: true
                }];
                for (var index in Config.columns) {
                    var item = Config.columns[index];
                    columns.push({
                        field: item["fieldcode"],
                        title: item["title"],
                        operate: 'LIKE'
                    });
                }
                columns.push({
                    field: 'createtime',
                    title: __('Createtime'),
                    formatter: Table.api.formatter.datetime
                });
                columns.push({
                    field: 'status',
                    title: __('Status'),
                    formatter: Table.api.formatter.status
                });
                columns.push({
                    field: 'operate',
                    title: __('Operate'),
                    table: table,
                    events: Table.api.events.operate,
                    formatter: Table.api.formatter.operate
                });

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'weigh',
                    fixedColumns: true,
                    fixedRightNumber: 1,
                    columns: [columns]
                });

                // 为表格绑定事件
                Table.api.bindevent(table);

                $(".btn-createOrUpdateXcformPage").click(function () {
                    xcflow.xcformcrud_openCreateOrUpdateXcformPage(true, "csmitsm/xcflowmdmdata/", Config.xcform.id, "");
                });
            },
            createorupdatexcformpage: function () {
                var that = this;
                var baseurl = "csmitsm/xcflowmdmdata/";
                new Vue({
                    el: '#app',
                    data: function () {
                        return {
                            fieldfulljson: Config.fieldfulljson,
                            xcform: Config.xcform,
                            control: {
                                form: {}
                            }
                        };
                    },
                    methods: {
                        control_clickSave: function () {
                            var that = this;
                            var data = xcflow.xcformcrud_getFormData(that.fieldfulljson);
                            xcflow.xcformcrud_saveFormData(baseurl, data, function () {
                                return true;
                            });
                        },
                        control_clickSubmit: function () {
                            var that = this;
                            var data = xcflow.xcformcrud_getFormData(that.fieldfulljson);

                            that.$refs["form"].validate((valid) => {
                                if (valid) {
                                    xcflow.xcformcrud_submitFormData(baseurl, data, function () {
                                        return true;
                                    });
                                }
                            });

                        }
                    }
                });
            },
            showxcformpage: function () {
                var that = this;
                var baseurl = "csmitsm/xcflowmdmdata/";

                new Vue({
                    el: '#app',
                    data: function () {
                        return {
                            fieldfulljson: Config.fieldfulljson,
                            xcform: Config.xcform,
                            display_createbutton: Config.display_createbutton,
                            control: {
                                form: {}
                            }
                        };
                    },
                    methods: {
                        control_clickModify: function () {
                            var that = this;
                            xcflow.xcformcrud_openCreateOrUpdateXcformPage(false, baseurl, Config.xcform.id, Config.id);
                        },
                        control_clickNew: function () {
                            var that = this;
                            var url = Fast.api.fixurl("csmitsm/xcflowmdmdata/createOrUpdateXcformPage?xcform_id=" + Config.xcform.id);
                            window.location.href = url;
                        }
                    }
                });
            }
        };
        return Controller;
    });